#!/bin/bash
echo "设置支持中文"
sudo chmod 777 /etc/locate.conf
cat>/etc/locate.conf<<EOF
LANG="zh_CN.UTF-8"
SYSFONT="latarcyrheb-sun16"
SUPPORTED="zh_CN.UTF-8:zh_CN:zh"
EOF

sudo yum install -y unzip wget
echo "设置开机不启动防火墙，以及开启启动docker"
sudo systemctl enable docker
sudo systemctl disable firewalld

echo "关闭防火墙，启动docker..."
sudo systemctl stop firewalld
sudo systemctl start docker

docker rm -f $(docker ps -aq)
sudo rm -rf /mydata && sudo mkdir -p /mydata/mysql/conf && sudo chmod 777 -R /mydata

echo "自定义mysql的配置文件..."
cat>/mydata/mysql/conf/my.cnf<<EOF
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
skip-name-resolve
EOF
sudo chmod 777 -R /mydata
cp -r /home/vagrant/share/sql/ /mydata/mysql/sql
docker network create --driver bridge nacos-net

echo "启动mysql..."
docker run -d -p 3306:3306 --name mysql \
-v /mydata/mysql/log:/var/log/mysql \
-v /mydata/mysql/data:/var/lib/mysql \
-v /mydata/mysql/conf:/etc/mysql \
-v /mydata/mysql/sql:/sql \
-e MYSQL_ROOT_PASSWORD=root \
--restart=always \
--network nacos-net \
mysql:5.7

sudo chmod 777 -R /mydata


echo "执行sql脚本创建数据库..."
sleep 15
docker exec  mysql mysql -uroot -proot -e "source /sql/gulimall_admin.sql"
docker exec  mysql mysql -uroot -proot -e "source /sql/gulimall_oms.sql"
docker exec  mysql mysql -uroot -proot -e "source /sql/gulimall_pms.sql"
docker exec  mysql mysql -uroot -proot -e "source /sql/gulimall_sms.sql"
docker exec  mysql mysql -uroot -proot -e "source /sql/gulimall_wms.sql"
docker exec  mysql mysql -uroot -proot -e "source /sql/gulimall_ums.sql"
docker exec  mysql mysql -uroot -proot -e "source /sql/nacos_config.sql"


docker run -d --name nacos01 nacos/nacos-server:1.1.4

sudo mkdir -p /mydata/nacos/conf  && sudo chmod 777 -R /mydata && sudo docker cp nacos01:/home/nacos/conf/ /mydata/nacos/conf && sudo mv /mydata/nacos/conf/conf/* /mydata/nacos/conf/ && sudo rm -rf /mydata/nacos/conf/conf/ && sudo docker cp nacos01:/home/nacos/logs /mydata/nacos/ && sudo mv /mydata/nacos/conf/application.properties /mydata/nacos/conf/application.properties.back && sudo chmod 777 -R /mydata
docker rm -f nacos01

echo "编辑nacos配置文件设置将数据持久化到mysql中..."
cat>/mydata/nacos/conf/application.properties<<EOF
spring.datasource.platform=mysql

db.num=1
db.url.0=jdbc:mysql://mysql:3306/nacos_config?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
db.user=root
db.password=root
EOF

echo "启动nacos..."
docker run --name nacos01 -d \
-p 8848:8848 \
--privileged=true \
--restart=always \
-e JVM_XMS=512m \
-e JVM_XMX=512m \
-e MODE=standalone \
-e PREFER_HOST_MODE=hostname \
-v /mydata/nacos/conf:/home/nacos/conf \
--network nacos-net \
nacos/nacos-server:1.1.4

sudo chmod 777 -R /mydata



echo "启动redis..."
mkdir -p /mydata/redis/conf && touch /mydata/redis/conf/redis.conf && echo "appendonly yes"  >> /mydata/redis/conf/redis.conf

docker run -p 6379:6379 --name redis \
-v /mydata/redis/data:/data \
-v /mydata/redis/conf/redis.conf:/etc/redis/redis.conf \
--restart=always \
-d redis redis-server /etc/redis/redis.conf